package com.ocean.web.controller.product;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.friendship.domain.TSFriendshipLink;
import com.ocean.friendship.service.ITSFriendshipLinkService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 友情链接Controller
 *
 * @author ocean
 * @date 2024-02-19
 */
@Controller
@RequestMapping("/friendship/link")
public class TSFriendshipLinkController extends BaseController
{
    private String prefix = "system/friendship";

    @Autowired
    private ITSFriendshipLinkService tSFriendshipLinkService;

    @RequiresPermissions("friendship:link:view")
    @GetMapping()
    public String link()
    {
        return prefix + "/link";
    }

    /**
     * 查询友情链接列表
     */
    @RequiresPermissions("friendship:link:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSFriendshipLink tSFriendshipLink)
    {
        startPage();
        List<TSFriendshipLink> list = tSFriendshipLinkService.selectTSFriendshipLinkList(tSFriendshipLink);
        return getDataTable(list);
    }

    /**
     * 导出友情链接列表
     */
    @RequiresPermissions("friendship:link:export")
    @Log(title = "友情链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSFriendshipLink tSFriendshipLink)
    {
        List<TSFriendshipLink> list = tSFriendshipLinkService.selectTSFriendshipLinkList(tSFriendshipLink);
        ExcelUtil<TSFriendshipLink> util = new ExcelUtil<TSFriendshipLink>(TSFriendshipLink.class);
        return util.exportExcel(list, "友情链接数据");
    }

    /**
     * 新增友情链接
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TSFriendshipLink> list = tSFriendshipLinkService.selectTSFriendshipLinkList(new TSFriendshipLink());
        List<String> linkNames = list.stream().map(TSFriendshipLink::getLinkName).collect(Collectors.toList());
        List<String> linkUrls = list.stream().map(TSFriendshipLink::getLinkUrl).collect(Collectors.toList());
        mmap.put("linkNames",linkNames);mmap.put("linkUrls",linkUrls);
        return prefix + "/add";
    }

    /**
     * 新增保存友情链接
     */
    @RequiresPermissions("friendship:link:add")
    @Log(title = "友情链接", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TSFriendshipLink tSFriendshipLink)
    {
        tSFriendshipLink.setCreateName(getSysUser().getUserName());
        tSFriendshipLink.setCreateBy(getLoginName());
        tSFriendshipLink.setCreateDate(new Date());
        return toAjax(tSFriendshipLinkService.insertTSFriendshipLink(tSFriendshipLink));
    }

    /**
     * 修改友情链接
     */
    @RequiresPermissions("friendship:link:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<TSFriendshipLink> list = tSFriendshipLinkService.selectTSFriendshipLinkList(new TSFriendshipLink());
        List<String> linkNames = list.stream().map(TSFriendshipLink::getLinkName).collect(Collectors.toList());
        List<String> linkUrls = list.stream().map(TSFriendshipLink::getLinkUrl).collect(Collectors.toList());
        mmap.put("linkNames",linkNames);mmap.put("linkUrls",linkUrls);
        TSFriendshipLink tSFriendshipLink = tSFriendshipLinkService.selectTSFriendshipLinkById(id);
        mmap.put("tSFriendshipLink", tSFriendshipLink);
        return prefix + "/edit";
    }

    /**
     * 修改保存友情链接
     */
    @RequiresPermissions("friendship:link:edit")
    @Log(title = "友情链接", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSFriendshipLink tSFriendshipLink)
    {
        tSFriendshipLink.setUpdateName(getSysUser().getUserName());
        tSFriendshipLink.setUpdateBy(getLoginName());
        tSFriendshipLink.setUpdateDate(new Date());
        return toAjax(tSFriendshipLinkService.updateTSFriendshipLink(tSFriendshipLink));
    }

    /**
     * 删除友情链接
     */
    @RequiresPermissions("friendship:link:remove")
    @Log(title = "友情链接", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tSFriendshipLinkService.deleteTSFriendshipLinkByIds(ids));
    }

    /**
     * 友情链接
     * @return
     */
    @PostMapping( "/yqlj")
    @ResponseBody
    public AjaxResult yqlj() {
        List<TSFriendshipLink> yqlj = tSFriendshipLinkService.getyqlj();
        return AjaxResult.success(yqlj);
    }
}
